/******************************************************************************************
 *                                                                                        *
 *									                                                      *
 *  American Mine Research Mine Messenger                                                 *
 *                                                                                        *
 ******************************************************************************************
 * FileName:        ReadMenu.c                                                      *
 * Dependencies:    See INCLUDES section below                                            *
 * Processor:       PIC32                                                                 *
 *                                                                                        *
 * Complier:        MPLAB C32 C Compiler v1.0                                             *
 *                  MPLAB IDE v8.0 or higher                                              *
 *                                                                                        *
 *                                                                                        *
 *								                                          				  *
 *                                                                                        *
 * Author(s)			    	Date         Version		Comment                       *
 ******************************************************************************************
 * Extreme Endeavors  		Oct 31, 2008 	   1.0		This is the C Code version        *
 *                                                      of AMR's Mine Tracker/Messenger   *
 *                                                                                        *
 ******************************************************************************************/



/***** INCLUDES **********************************************************/

// Standard Includes
	#include <p32xxxx.h>		// Using this generic file helps portability - it finds the right file
	#include <plib.h>
	#include "stdio.h"
	#include "stdlib.h"
	#include "symbols.h"
	#include "db_utils.h"


// Includes for Subroutine Access
	#include "local_system.h"
	#include "MainMenu.h"
	#include "ReadMenu.h"
	#include "UserIO.h"				//  For the defines of the switches and LEDs
	#include "Display.h"
	#include "Contact_List.h"
	#include "Store_Retrieve_Message.h"
//	#include "Menu.h"
	#include "charge.h"
	#include "LabelButtons.h"
	#include "rtcc.h"

#ifdef ReadMenu_H
/***** VARIABLES ********************************************************/


/***** FUNCTION PROTOTYPES ******************************************************/


//void Read_Message(char time_rx[], char from_who[], char message_string_in[] );
void Read_Message(void);
void Display_Message_From_Memory(unsigned char message_number);
void Convert_To_Ascii (char ret[], int input);
char Browse_Messages_Menu(unsigned char rx_message_counts[]);

#pragma code
/****************************************************
 *                                                  *
 *            Start of MAIN PROGRAM                 *
 *                                                  *
 ***************************************************/

		
	
//void Read_Message(char time_rx[], char from_who[], char message_string_in[] )
void Read_Message(void)
{

	unsigned char rx_message_cnts[30];
	char return_array[18];
	char message_select = 0, number_of_stored_messages = 0;

	// variable initialization.
	number_of_stored_messages = Browse_Messages_Menu(rx_message_cnts);

	m=0;


	while(!IFS0bits.T3IF)
	{
			if(!SW1)
			{
				DebounceProtect(1); 				// Wait to act until the person lets up on the button
				WriteTimer23(0x00000000);			//Reset Timer23 to start count over since a button what hit
				ReturnToMainMenu();
				return;
			}
			if(!SW7)
			{
				DebounceProtect(7);
				WriteTimer23(0x00000000);
				Display_Message_From_Memory(rx_message_cnts[message_select]);
				number_of_stored_messages =  Browse_Messages_Menu(rx_message_cnts);
			}
		
			if(!SW2)							// erase messages if this button selected.
			{
				DebounceProtect(2);
				WriteTimer23(0x00000000);
				Erase_Messages();
				gselvp(8);
				gclrvp();
			}

			if(!SW3)
			{
				DebounceProtect(3); 
				WriteTimer23(0x00000000);	
				message_select--;
				if(message_select >= 0)
				{
					Put_Box_Around(message_select+1, 0);
					Put_Box_Around(message_select, 1);
				}	
				else
				{
					message_select = number_of_stored_messages;
					Put_Box_Around(0,0);
					Put_Box_Around(message_select, 1);
				}

			}

			if(!SW5)
			{
				DebounceProtect(5); 
				WriteTimer23(0x00000000);
				message_select++;
				if(message_select > number_of_stored_messages)
				{
				  message_select = 0;
				  Put_Box_Around(number_of_stored_messages, 0);
				  Put_Box_Around(message_select, 1);
				}	
				else
				{
					Put_Box_Around(message_select-1, 0);
					Put_Box_Around(message_select, 1);
				}
			}



	}		


	return;
}



void Display_Message_From_Memory(unsigned char message_number)
{
	unsigned char stored_message[47], i, j;
	unsigned char display_msg[36];


	for(i=0; i<35; i++)
	{
			display_msg[i] = 0;
	}


	PGCSTR b1, b2, b3, b4, b5, b6, b7;


	// This sets the screen for the Read MSG button.
	// Transition times are long with this right now - they need shortened.	
	b1 = "RETURN";
	b2 = " ";
	b3 = " ";
	b4 = " ";
	b5 = " ";
	b6 = " ";
	b7 = " ";

	// Write the new menu
	QuickLabelButtons(b1, b2, b3, b4, b5, b6, b7);
	gselvp(8);
	gclrvp();



		Retrieve_Stored_Message(message_number,stored_message);
				// This retrieves the last message from memory allowing the 
				// extraction of the source and date&time bytes for display
				// work will have to be done in order to retrieve multiple 
				// messages from memory and display the name/date recieved of each
		i = 0;
		while(i<31)
		{
			display_msg[i] = stored_message[16+i];
			j = i;
			if(stored_message[17+i] == 0x17)
			{
				i =32;
			}	
				i ++;	
		}

		Display_Text_VPort8_Medfont(display_msg, j);

		while(!IFS0bits.T3IF)
		{
			if(!SW1)
			{
				DebounceProtect(1); 				// Wait to act until the person lets up on the button
				WriteTimer23(0x00000000);			//Reset Timer23 to start count over since a button what hit
				return;
			}
		}
}



void Convert_To_Ascii (char ret[], int input)
{
	int holder;
	holder = input * .001;
	ret[1] = (holder | 0x30);
	input = input - (holder* 1000) ;
	holder = input*.01 ;
	ret[2] = (holder | 0x30);
	input = input - (holder* 100) ;
	holder = input*.1;
	ret[3] = (holder | 0x30);
	input = input - (holder* 10) ;

	ret[4] = (input | 0x30);



return;

}



char Browse_Messages_Menu(unsigned char rx_message_counts[])
{
	PGCSTR b1, b2, b3, b4, b5, b6, b7;
	unsigned char i = 0, k =0;
	unsigned char no_of_message[1];
	unsigned char stored_message[47];
	int sender;
//	unsigned char rx_message_counts[30];
	char return_array[18];
	char message_select = 0, number_of_stored_messages = 0;

	// This sets the screen for the Read MSG button.
	// Transition times are long with this right now - they need shortened.	
	b1 = "RETURN\nTo\nMain";
	b2 = "Erase\nAll";
	b3 = "UP ";
	b4 = " ";
	b5 = "DOWN";
	b6 = " ";
	b7 = "READ\nMSG";

	// Write the new menu
	QuickLabelButtons(b1, b2, b3, b4, b5, b6, b7);
	gselvp(8);
	gclrvp();



	Build_Message_Number_Array(rx_message_counts);
	
	return_array[0] = 0x20; 

	while(k<30)
	{
	if( ! (rx_message_counts[k] == 255))
			{
				Retrieve_Stored_Message(rx_message_counts[k],stored_message);
				// prind info to viewport 8
				sender = ( stored_message[0] << 8 )  | stored_message[1] ;
				Convert_To_Ascii(return_array, sender);
			
				return_array[5] = 0x20; 
		   
				return_array[6] =  ( stored_message[5] >>4) | 0x30 ;    // place the msd of the month
				return_array[7] =  ( stored_message[5] & 0x0F ) | 0x30 ;	// plase the LSD fo the month
				return_array[8] = '/';
	
				return_array[9] =  ( stored_message[4] >>4) | 0x30 ;    // place the msd of the DAY
				return_array[10] =  ( stored_message[4] & 0x0F ) | 0x30 ;	// plase the LSD fo the DAY
				return_array[11] = 0x20;


				return_array[12] =  ( stored_message[3] >>4) | 0x30 ;    // place the msd of the hour
				return_array[13] =  ( stored_message[3] & 0x0F ) | 0x30 ;	// plase the LSD fo the hour
				return_array[14] = ':';

				return_array[15] = ( stored_message[2] >>4) | 0x30 ;    // place the msd of the Minute
				return_array[16] = ( stored_message[2] & 0x0F ) | 0x30 ;	// plase the LSD fo the Minute

				return_array[17] = '\n';

			Display_Text_VPort8_smallfont(return_array, 18);
	
				k++;
				number_of_stored_messages++;

			}
			else
			{
				k=30;
			}
	}

		number_of_stored_messages--;			//  Decrement our message count by one since the count starts at 0 instead of one for hte position of the box.

		//  Color is 0= black**** 1= red**** 2=Green  ***** 3= Blue  *** 4 = Yellow
		//  Position of zero thru nine
		gsetpos(0,0);
		Put_Box_Around(message_select, 1);

	return number_of_stored_messages;	
}

#endif


